<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <title>简易管理平台</title>
    <link rel="shortcut icon" href="__PUBLIC__/favicon.ico">
    <include file="views/layout/template/head"/>
    <style type="text/css">
        .layui-btn {
            margin-left: 5px;
        }
        .xw__grid img {
            height: 30px;
        }
        .file-img {
            height: 100%;
            background-position: center;
            background-size: contain;
            background-repeat: no-repeat;
            cursor: pointer;
        }
        img {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body style="overflow: hidden;">{$PHP_ACTION}
    <control:grid id="grid" padding="0" height="100%" xml="public/base/controls/upload/config.xml"/>
    <script type="text/javascript">
        var page = (function(ns) {

            var uploadObj;

            //页面初始化
            ns.init = function() {
                var filter = [];
                if($.getRequest("fkGuid"))
                    filter.push({ "field": "file_guid", "opera": "=", "value": $.getRequest("fkGuid") });
                if($.getRequest("ywName"))
                    filter.push({  "field": "file_buss_type", "opera": "=", "value": $.getRequest("ywName") });
                if($.getRequest("fkType"))
                    filter.push({ "field": "file_type", "opera": "=", "value": $.getRequest("fkType") });
                grid.load(filter);
                grid.loadSuccess(function() {
                	if($.getRequest("actionCode") != 3){	
                    	grid.setBtnIsShow("*", true);
                	}
		 		    if($.getRequest("max") > 0 &&  grid.getRows().length >= $.getRequest("max") ){
		 		    	$(".layui-btn").eq(0).hide();
		 		    	return false;
		 		    }
                });
                ns.initUpload();
            }

            //初始化上传按钮
            ns.initUpload = function() {
                uploadObj = layui.uploads.init({
                    id: "btnUpload",
                    fkGuid: $.getRequest("fkGuid"),
                    ywName: $.getRequest("ywName"),
                    fkType: $.getRequest("fkType"),
                    multiple: $.getRequest("multiple") || true,
                    mode: $.getRequest("mode") || 1,
                    errorInfo: $.getRequest("errorInfo") || '请先选择',
                    showType: $.getRequest("showType") || "grid",
                    createBtn: false,
	    			min:$.getRequest("min") || 0,
	    			max:$.getRequest("max") || 0,
	    			accept:$.getRequest("file") || 'file',		//允许上传的文件类型
                });
                uploadObj.uploadSuccess(function(res) {
                    ns.reset();
                });
                ns.openerObj = (window.opener && window.opener.$("#" + $.getRequest("id"))) ||
                    (window.parent && window.parent.$("#" + $.getRequest("id"))) || false;
                if($.getRequest("actionCode") == 3 || !ns.openerObj) {
                    uploadObj.setActionCode(3);
                } 
            }

            //下载
            ns.download = function() {
                var selectedRows = grid.getSelectedRows();
                if(!selectedRows[0]) {
                    $.toast(0, "请选择文件！");
                    return;
                }
                var fileids = $.map(selectedRows, function(h) {
                    return h.file_id
                }).join(',');
                var url = __root__ + "/index.php/admin/soft/upload/downloadHandler?file_id=" + fileids + "&is_ziz=true";
                window.open(url);
            }

            //查看
            ns.see = function(file_id) {
                var url = __root__ + "/index.php/admin/soft/upload/downloadHandler?file_id=" + file_id;
                window.open(url);
            }

            //删除
            ns.del = function() {
                var selectedRows = grid.getSelectedRows();
                if(!selectedRows[0]) {
                    $.toast(0, "请选择需要删除的行！");
                    return;
                }
                let count = grid.getRows().length;
                let min = $.getRequest("min");
	 		    if(min > 0 && count - selectedRows.length < min){
	 		    	$.toast(0,"附件不能少于"+min);
	 		    	return false;
	 		    }
                $.confirm("确认删除已选数据？", function() {
                    var fileIds = $.map(selectedRows, function(h) {
                        return h.file_id
                    }).join(',');
                    $.ajaxs("/admin/soft/upload/deleteFile", {
                        fileIds: fileIds
                    }, function(res) {
                        $.toast(1, "删除成功！");
                        ns.reset();
                    });
                });
            }

            //刷新
            ns.reset = function() {
                grid.load();
                var openerObj = ns.openerObj;
                if(openerObj && openerObj.length > 0 && openerObj.data("obj")) {
                    openerObj.data("obj").resetFileBtnCount();
                }
            }

            return ns;
        }({}));
        
        $(function(){ page.init() });
    </script>
</body>
</html>